package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Destination;
import cn.wolfcode.wolf2w.business.api.domain.Region;
import cn.wolfcode.wolf2w.business.mapper.DestinationMapper;
import cn.wolfcode.wolf2w.business.mapper.RegionMapper;
import cn.wolfcode.wolf2w.business.query.DestinationQuery;
import cn.wolfcode.wolf2w.business.service.IDestinationService;
import cn.wolfcode.wolf2w.business.vo.DestinationVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.lang.model.element.PackageElement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * 目的地Service业务层处理
 *
 * @author 沙琪玛
 * @date 2024-11-01
 */
@Service
@Transactional
public class DestinationServiceImpl extends ServiceImpl<DestinationMapper, Destination> implements IDestinationService {

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public IPage<Destination> queryPage(DestinationQuery qo) {
        IPage<Destination> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<Destination> searchByRegionId(Long regionId) {
        List<Destination> list = null;
        if (regionId == -1) {
            //查国内
            list = lambdaQuery().eq(Destination::getParentName, "中国")
                    .list();
        } else {
            Region region = regionMapper.selectById(regionId);
            String refIds = region.getRefIds();
            String[] split = refIds.split(",");
            list = new ArrayList<>();
            for (String s : split) {
                //Destination表的id 根据这个id查城市
                Long id = Long.valueOf(s);
                Destination dest = lambdaQuery().eq(Destination::getId, id)
                        .one();
                list.add(dest);

            }
        }

        for (Destination destination : list) {
            Long parentId = destination.getId();
            List<Destination> children = lambdaQuery().eq(Destination::getParentId, parentId)
                    .last("limit 10")
                    .list();
            destination.setChildren(children);
        }
        return list;
    }

    @Override
    public List<Destination> queryToasts(Long destId) {
        //使用递归方式查询目的地列表
        List<Destination> list = new ArrayList<>();
        createToast(destId, list);

        return list;
    }

    @Override
    public List<DestinationVO> querylist(Long type) {
        List<Destination> list = null;
        List<DestinationVO> destinationVOList = new ArrayList<>();
        if (type == -1) {
            //查国内
            list = lambdaQuery().eq(Destination::getParentName, "中国")
                    .list();
        } else {
            Region region = regionMapper.selectById(type);
            String refIds = region.getRefIds();
            String[] split = refIds.split(",");
            list = new ArrayList<>();
            for (String s : split) {
                //Destination表的id 根据这个id查城市
                Long id = Long.valueOf(s);
                Destination dest = lambdaQuery().eq(Destination::getId, id)
                        .one();
                list.add(dest);

            }
        }
        for (Destination destination : list) {
            DestinationVO dest = new DestinationVO();
            dest.setId(destination.getId());
            dest.setName(destination.getName());
            LoadCIty(destination.getId(),dest.getChildren());
            destinationVOList.add(dest);
        }
        return destinationVOList;
    }


    private void createToast(Long destId, List<Destination> list) {
        if (destId == null) {
            return;
        }
        Destination destination = baseMapper.selectById(destId);
        createToast(destination.getParentId(), list);
        list.add(destination);

    }

    private void LoadCIty(Long destId, List<DestinationVO> list) {
        List<Destination> destinationList = lambdaQuery().eq(Destination::getParentId, destId).list();
        if (destinationList.size()==0){
            return;
        }
        for (Destination destination : destinationList) {
            DestinationVO dest = new DestinationVO();
            dest.setId(destination.getId());
            dest.setName(destination.getName());
            LoadCIty(destination.getId(),dest.getChildren());
            list.add(dest);
        }

    }
}
